﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Document_Management
{
    public partial class FrmSearchFactoryMnt : Form
    {
        static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        TextBox tb;
        public FrmSearchFactoryMnt(TextBox tb)
        {
            InitializeComponent();
            this.tb = tb;
        }

        private void FrmSearchiMageMnt_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                string strcmd = "sp_圖面廠區_尋找";
                //string strcmd = " Select * From 圖面廠區";
                SqlDataAdapter cmd = new SqlDataAdapter(strcmd, con);
                cmd.SelectCommand.CommandType = CommandType.StoredProcedure;
                //cmd.SelectCommand.Parameters.AddWithValue("@廠區名稱","f");
                DataTable dt = new DataTable();
                cmd.Fill(dt);
                dataGV廠區Search.DataSource = dt;
                PublicClass.setRowNumber(dataGV廠區Search);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            try
            {
                int r = dataGV廠區Search.CurrentCell.RowIndex;
                string sup廠區Id = dataGV廠區Search.Rows[r].Cells[0].Value.ToString();
                tb.Text = sup廠區Id;
                this.Close();
            }
            catch
            {

            }
        }

        private void btn_search_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                string strcmd = "sp_圖面廠區_尋找";
                SqlDataAdapter cmd = new SqlDataAdapter(strcmd, con);
                cmd.SelectCommand.CommandType = CommandType.StoredProcedure;
                //if (txt廠區.Text != "") cmd.SelectCommand.Parameters.AddWithValue("@廠區代號", txt廠區.Text);
                if (txt廠區.Text != "") cmd.SelectCommand.Parameters.AddWithValue("@廠區代號或名稱", txt廠區.Text);
                //@廠區代號
                //@廠區名稱
                DataTable dt = new DataTable();
                cmd.Fill(dt);
                dataGV廠區Search.DataSource = dt;
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void dataGV廠區Search_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGV廠區Search);
        }

        private void dataGV廠區Search_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGV廠區Search);
        }
    }
}
